package com.zzr.demo.redis.controller;

import com.zzr.demo.redis.service.ProbabilityGroupCountService;
import com.zzr.demo.redis.service.impl.HLLGroupCountServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.UUID;

/**
 * @author: xiaomi
 * @date: 2021/10/10
 * @description:
 */
@RestController
@RequestMapping("group/count/quarter/")
@Api(tags = "季度统计：使用 HyperLogLog完成季度近似基数的统计")
@Slf4j
public class QuarterCountController {

    int[] months = new int[]{1, 2, 3};
    @Autowired
    ProbabilityGroupCountService probabilityGroupCountService;
    Random random = new Random();
    final String groupName = "quarter";

    @GetMapping("/increase")
    @ApiOperation("随机计数在月份上")
    public boolean increase() {
        int i = random.nextInt(3);
        ProbabilityGroupCountService.iptl.set(UUID.randomUUID().toString());
        probabilityGroupCountService.increase(groupName, months[i] + "");
        return true;
    }

    @GetMapping("/sizeGroup")
    @ApiOperation("统计季度的（近视）基数")
    public long sizeGroup() throws InterruptedException {
        long size = probabilityGroupCountService.size(groupName);
        return size;
    }
}
